﻿using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class SJHVA06
    {
        public ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        #region ==EXORD===========
        public static readonly DependencyProperty EXORDProperty = DependencyProperty.Register("EXORD",
            typeof(string), typeof(SJHVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXORDChanged));
        public string EXORD
        {
            get
            {
                return (string)GetValue(EXORDProperty);
            }
            set
            {
                SetValue(EXORDProperty, value);
                OnEXORDChg();
            }
        }
        private static void OnEXORDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHVA06;
            if (src != null)
            {
                src.EXORD = (string)e.NewValue;
            }
        }
        public virtual void OnEXORDChg()
        { }
        #endregion 
        #region ==WFART===========
        public static readonly DependencyProperty WFARTProperty = DependencyProperty.Register("WFART",
            typeof(string), typeof(SJHVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWFARTChanged));
        public string WFART
        {
            get
            {
                return (string)GetValue(WFARTProperty);
            }
            set
            {
                SetValue(WFARTProperty, value);
                
            }
        }
        private static void OnWFARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as SJHVA06;
            if (src != null)
            {
                src.WFART = (string)e.NewValue;
                src.OnWFARTChg((string)e.NewValue);
            }
        }
        public void OnWFARTChg(string wfart)
        {
            EXORD = "";
            if (wfart.Length == 0)
                return;

            MCO.dwData = MCO.TBMain.NewRow();
            MCO.dwData["MANDANT"] = MCO.app.Mandant;
            MCO.dwData["KRS"] = "KA";
            MCO.dwData["ART"] = wfart.PadRight(38).Substring(18, 20);
            MCO.dwData["VAR"] = "   ";
            MCO.dwData["FARBE"] = "1".PadRight(10);
            MCO.SResult = MCO.QMCO04(MCO.dwData);
            foreach (DataRow row in MCO.SResult.Tables[0].Rows)
            {
                EXORD = row["KRS"].ToString() + row["LFD"].ToString() + row["POS"].ToString();
                exauf = EXORD;
                break;
            }
        }
        #endregion

        
        ITXABPLANData TAB = new ITXABPLANData();
        VICC_ABPOS_MCO MCO = new VICC_ABPOS_MCO();
        VICC_ABAUFM VMUI = new VICC_ABAUFM();
        public SJHVA06()
        {  
            InitializeComponent();
            this.Name = "品种管理";
            SetUp();
        }

        private void SetUp()
        {
            //默认注释随客户不同而不同，所以放在这里
            //根据01-，02-，03-等等对备注类型进行分类。分类方法可以依据客户不同自己定义，主要用于报表和显示。例如01如果代表成份，则可以在打印时处理。保存在TXTKON表的某个自duang
            List<string> deftxt = new List<string>();
            deftxt.Add("01-备注:");
            cVA.DEFWSTXT = deftxt;
            deftxt = new List<string>();
            deftxt.Add("00-please type your remark after remove this:");
            cVA.DEFTXT = deftxt;
            cVA.VALBTYP = "TSTVALB";
            cVA.IsExtProd = true;
            cRZ.RUNID = this.RUNID;
        }
        string exauf="";
        private void StartABCommand(object sender, RoutedEventArgs e)
        {
            if (WFART == "")
                return;
            string wfart = "F" + Helper.Left(WFART.Substring(18, 24).PadRight(20), 20)
                            + Helper.Left("".PadRight(2), 2)
                            + Helper.Left("1".PadRight(10), 10)
                            + Helper.Left("".PadRight(10), 10)
                             + Helper.Left("", 10)
                            + Helper.Left("", 10)
                               + "".PadRight(7)
                            + Helper.Left(Helper.ConvertGB2312To8859P1("".PadRight(20)), 20);
            string pos = VSDDAT.SD22.GETNEXT("0022", "KA", WFART.Substring(2, 6), 1, 3);
              exauf = Helper.Left(("KA" + WFART.Substring(2, 6) + pos).PadRight(11), 11)
                      + wfart
                       + Helper.Left("".PadRight(8), 8)
                       + Helper.Left("", 20)
                       + Helper.Left("", 20);

            try
            {

                IsChanged = false;
                Dictionary<string, object> PM = cRZ.PM;
                PM["RUNID"] = this.RUNID;
                PM["KRS"] = exauf.Substring(0, 2);
                PM["AUFART"] = "VA";
                PM["ORDERNUM"] = exauf.Substring(0, 11);
                PM["LFD"] = exauf.Substring(2, 6);
                PM["RELOADABK"] = false;
                string ABAUF = cRZ.TMV.VABK.NEWABK(PM);

                PM["ORDERNUM"] = ABAUF.Substring(0, 8) + exauf.Substring(8, 3) + "VA ";
                PM["COLOR"] = "VA ";
                PM["AUFTYP"] = "V";
                PM["ARTSTAGE"] = "Z";
                //PM["VKAUF"] = pm["ORDERNUM"];
                PM["VKPAUF"] = ABAUF.Substring(0, 8) + exauf.Substring(8, 3);
                PM["VKPQTY"] = 1000;//
                PM["VKAUF"] = "                  ";//
                PM["QTYEND"] = 1000;
                PM["UNDES"] = "HZ000001";
                PM["DELDATE"] = cRZ.TMV.date;
                PM["STARTDATE"] = cRZ.TMV.date;
                PM["P6"] = exauf.Substring(129, 10);//PARTIE
                bool IsExists = cRZ.TMV.VPR.NEWPLAN(PM); 
                MessageInfo msg = new MessageInfo();
                msg.Sender = this.RUNID;
                msg.MSGTYPE = "RZABPNEW";
                msg.ACTION = "RZABPNEW";
                msg.AUFTYP = "V";
                msg.WAKZ = exauf.Substring(11, 1);
                msg.ART = exauf.Substring(12, 20);
                msg.VAR = exauf.Substring(32, 2).PadRight(3);
                msg.FARBE = exauf.Substring(34, 10);
                msg.BESCHICH = exauf.Substring(44, 10);
                msg.DESS = exauf.Substring(54, 10);
                msg.COLORIT = exauf.Substring(64, 10);
                msg.ORDER = (string)PM["ORDERNUM"];
                msg.Where = app.User;
                VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);  
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnChanged(bool value)
        {
            if (!value)
                return;
            EXORD = "";
            EXORD = exauf.Substring(0,11);
            INFO = "初始化已经完成,可以进行评估";
            base.OnChanged(value);
        }
        private void StartKACommand(object sender, RoutedEventArgs e)
        {
            try
            {
                cVA.IsSaved = false;
                cVA.IsSaved = true;
                string krs = exauf.Substring(0, 2);
                string lfd = exauf.Substring(2, 6);
                string pos = exauf.Substring(8, 3);
                Dictionary<string, string> list = (Dictionary<string, string>)TAB.GETON(krs, lfd, pos);
                string key = krs + "_" + lfd + "_" + pos;
                list.Add(key, "YES");
                //删除预估结果
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);

                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.ResetMCO(MCO.PM);//删除本身
                    VMUI.ResetMAT(MCO.PM);//删除原料
                }
                //插入需求关系
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);

                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.PrepareProdCost(MCO.PM);//插入本身
                    VMUI.PrepareMatCost(MCO.PM);//插入原料
                }
                //计算经纱成本
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);
                    if (krs != "SA") continue;
                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.UpdateProdCost(MCO.PM);//经轴加工费
                    VMUI.UpdateMatCost(MCO.PM);//经纱成本
                    MCO.CalculateCost(MCO.PM);//经轴总费用，并更新到织造费用
                }
                //计算纬纱
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);
                    if (krs != "WA") continue;
                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.UpdateProdCost(MCO.PM);//织造加工费
                    VMUI.UpdateMatCost(MCO.PM);//纬纱成本
                    MCO.CalculateCost(MCO.PM);//坯布总费用，并更新到成品
                }
                foreach (KeyValuePair<string, string> kp in list)
                {
                    key = kp.Key;
                    krs = key.Substring(0, 2);
                    lfd = key.Substring(3, 6);
                    pos = key.Substring(10, 3);
                    if (krs != "KA") continue;
                    MCO.PM["KRS"] = key.Substring(0, 2);
                    MCO.PM["LFD"] = key.Substring(3, 6);
                    MCO.PM["POS"] = key.Substring(10, 3);
                    MCO.UpdateProdCost(MCO.PM);//整理加工费
                    VMUI.UpdateMatCost(MCO.PM);//坯布成本
                    MCO.CalculateCost(MCO.PM);//整理总费用
                }
                key = EXORD;
                EXORD = "";
                EXORD = key;
                INFO = "评估已经完成";

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
    }
}